package org.mili.ticketeer.report;

import org.mili.core.templating.TemplateStore;
import org.mili.ticketeer.dto.Group;
import org.mili.ticketeer.dto.Ticket;
import org.mili.ticketeer.service.ServiceException;

import java.util.List;
import java.util.Map;

public class AbstractTicketGenerator extends AbstractGenerator {
    public AbstractTicketGenerator(ReportEngine reportEngine, TemplateStore templateStore) {
        super(reportEngine, templateStore);
    }

    protected void doWithTickets(TicketJob ticketJob) throws ServiceException {
        for(Map.Entry<Group, List<Ticket>> entry : reportEngine.getTicketsByGroups().entrySet()) {
            ticketJob.execute(entry.getKey(), entry.getValue());
        }
    }

    protected String replaceGroup(String s, Group group, StringBuilder content) {
        s = s.replace("${group_name}", reportEngine.getHtml(group.getName()))
                .replace("${Group_Item}", content);
        return s;
    }

    public static interface TicketJob {
        void execute(Group group, List<Ticket> tickets) throws ServiceException;
    }

}
